﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Aij1.Utils;
using Aij1.Model;
using Aij1.BLL;
using DevExpress.Xpo;
using Newtonsoft.Json;
using System.IO;

namespace Aij1
{
    /// <summary>
    /// AddComments 的摘要说明
    /// </summary>
    public class AddComments : IHttpHandler, IRequiresSessionState
    {
        C_Customer C;
        Session session;
        A_Article Article;
        HttpContext Context;
        public void ProcessRequest(HttpContext context)
        {
            session = XpoHelper.GetNewSession();
            Context = context;
            int cid = CommOp.ToInt(context.Request.QueryString["CID"]);
            string comment = CommOp.ToStr(context.Request.Form["Comment"]);
            C = AuthData.GetCurrentCustomer(session);
            Article = session.GetObjectByKey<A_Article>(cid);
            if (C != null && cid > 0 && !String.IsNullOrEmpty(comment))
            {
                AddComment(comment);
            }
            if (cid > 0)
            {
                WriteComment();
            }
        }

        protected void AddComment(string comment)
        {
            if (string.IsNullOrEmpty(comment)) return;

            A_Comment commentObj = new A_Comment(session)
            {
                Comment = comment,
                ArticleId = Article,
                EditorId = C,
                EditTime = DateTime.Now,
            };
            commentObj.Save();
        }

        protected void WriteComment()
        {
            int idx = 1;
            var comments = CommentData.GetComments(Article).Select(cmt => new
            {
                level = idx++,
                name = cmt.EditorId.SysName,
                time = cmt.EditTime.ToString("yyyy-MM-dd HH:mm"),
                comment = CommOp.ToHtmlStr(cmt.Comment),
            });
            string jsonComments = ToJson(comments);

            Context.Response.ContentType = "text/json";
            Context.Response.Write(jsonComments);
            Context.Response.End();
        }

        /// <summary>
        /// 序列化数据为Json数据格式.
        /// </summary>
        /// <param name="value">被序列化的对象</param>
        /// <returns></returns>
        public static string ToJson(object value)
        {
            Type type = value.GetType();
            Newtonsoft.Json.JsonSerializer json = new Newtonsoft.Json.JsonSerializer();
            json.NullValueHandling = NullValueHandling.Ignore;
            json.ObjectCreationHandling = Newtonsoft.Json.ObjectCreationHandling.Replace;
            json.MissingMemberHandling = Newtonsoft.Json.MissingMemberHandling.Ignore;
            json.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            StringWriter sw = new StringWriter();
            Newtonsoft.Json.JsonTextWriter writer = new JsonTextWriter(sw);
            writer.Formatting = Formatting.None;
            writer.QuoteChar = '"';
            json.Serialize(writer, value);

            string output = sw.ToString();
            writer.Close();
            sw.Close();
            return output;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}